<?php

class Register extends CI_Controller{

	function __construct(){
		parent::__construct();
		$this->load->helper(array('form', 'url'));
		$this->user_authentication->logged_in('dashboard/main', 'member');
	}

	function index($menu='pricing'){
		$this->data['menu'] = $menu;
		$this->load->view('homepage/main', $this->data);
	}
	
	function signup($type){
		$this->load->library('form_validation');
		
		$this->form_validation->set_rules('user_name', 'username', 'required|min_length[5]|max_length[12]|is_unique[user.user_name]');
		$this->form_validation->set_rules('user_first_name', 'nama depan', 'required');
		$this->form_validation->set_rules('user_last_name', 'nama belakang', 'required');
		$this->form_validation->set_rules('user_address', 'alamat', 'required');
		$this->form_validation->set_rules('user_password', 'password', 'required');
		$this->form_validation->set_rules('passconf', 'konfirmasi password', 'required|matches[user_password]');
		$this->form_validation->set_rules('user_email', 'email', 'required|valid_email|is_unique[user.user_email]');

		$this->form_validation->set_message('min_length', '% minimal 5 karakter, maksimal 12 karakter');
		$this->form_validation->set_message('max_length', '% minimal 5 karakter, maksimal 12 karakter');
		$this->form_validation->set_message('required', '%s harus diisi');
		$this->form_validation->set_message('valid_email', 'format %s tidak sesuai');
		$this->form_validation->set_message('matches', '%s tidak sama');
		$this->form_validation->set_message('is_unique', '%s sudah terdaftar, harap pakai yang lain');
		
		$this->data['menu'] = 'signup';
		$this->data['type'] = $type;
		
		if ($this->form_validation->run() == FALSE){
			$this->load->view('homepage/main', $this->data);	
		}else{
			$this->submit_signup();
		}
	}
	
	function submit_signup(){
		$value['user_name'] = $this->input->post('user_name');
		$value['user_first_name'] = $this->input->post('user_first_name');
		$value['user_last_name'] = $this->input->post('user_last_name');
		$value['user_address'] = $this->input->post('user_address');
		$value['user_email'] = $this->input->post('user_email');
		$value['user_password'] = md5($this->input->post('user_password'));
		$value['package_type'] = $this->input->post('pricing');
		$value['payment_method'] = 1;
		
		$this->load->model('m_user');
		$res = $this->m_user->insert_user($value);

		if($res) {
			$member = $this->m_user->get_one_by_username($value['user_name']);
			
			$this->session->set_userdata('payment', $member);
			
			$this->data['pricing'] = $value['package_type'];
			$this->data['menu'] = "payment_process";
			$this->data['confirm'] = 'Proses pendaftaran berhasil';
			
			$this->load->view('homepage/main', $this->data);			
		}
		else show_error('Registrasi Gagal');
	}
	
	function payment(){
		$type = $this->input->post('pricing');
		$amount = $this->get_amount($type);
		
		$resArray = $this->paypal->CallShortcutExpressCheckout($amount, 'USD', 'Sale', base_url().PAYPAL_RETURN_URL, base_url());
		$ack = strtoupper($resArray["ACK"]);
		if($ack=="SUCCESS" || $ack=="SUCCESSWITHWARNING")
		{
			$token = urldecode($resArray["TOKEN"]);
			$_SESSION['reshash']=$token;
			$this->paypal->RedirectToPayPal($token);
		}
	}
	
	function confirm(){		
		$session_data = $this->session->userdata('payment');
		
		$value['user_id'] = $session_data['user_id'];
		$data['user_status'] = 1;
				
		$this->load->model('m_user');
		$res = $this->m_user->update_user($data, $value);
		
		$value['payment_token'] = $this->input->get('token');
		$value['payer_id'] = $this->input->get('PayerID');
		
		$this->load->model('m_payment_info');
		$res = $this->m_payment_info->insert_payment($value);
		
		redirect('homepage/main/login');
	}
	
	function get_amount($type){
		switch ($type){
			case '1': return 10; break;
			case '2': return 20; break;
			case '3': return 30; break;
			case '4': return 40; break;
		}
	}
	
	function get_request(){
		$this->library->request_token();
	}
}